package xx.caffine.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import xx.caffine.dao.UserDao;
import xx.caffine.entity.User;
import xx.caffine.service.UserService;

/**
 * (User)表服务实现类
 *
 * @author halo-king
 * @since 2023-07-03 16:47:26
 */
@Service("userService")
@CacheConfig(cacheNames = "userCache",cacheManager = "caffeineCacheManager")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Cacheable( key = "#id")
    @Override
    public User queryGetById(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @CachePut(key = "#result.id")
    @Override
    public User insert(User user) {
        this.baseMapper.insert(user);
        return user;
    }

    @CachePut(key = "#result.id")
    @Override
    public User update(User user) {
        this.baseMapper.updateById(user);
        return user;
    }

    @CacheEvict(key = "#id")
    @Override
    public Integer delete(Integer id) {
        return this.baseMapper.deleteById(id);
    }
}
